<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="viewport" id="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no"/>
<base th:href="${#request.getContextPath()}+'/'">
<link href="css/reset.css" rel="stylesheet" type="text/css">
<link href="css/receiving-adress.css" rel="stylesheet" type="text/css">

</head>
<body>
<div class="near-box">
	<div class="header">
		<a href="javascript:history.go(-1)" class="left"></a>
		收货地址
	</div>
	<div class="bigbox">

         <a id="addAddress" th:href="@{/api/user/toAddAdress(memberId=${request.memberId})}" class="set-text1">添加新地址</a>
		 <!--确认删除弹层-->
		 <div class="delete-layer-bg" style="display: none;"></div>
		 <div class="delete-layer" style="display: none;">
	         <div class="active-a2">确认后地址将被删除！</div>
	         <div class="active-a3">
	        	<span class="other2 other1">取消</span>
	        	<span class="other2" id="deletebox">确定</span>
	        	<div class="active-a3-x1"></div>
	         </div>
		 </div>
	</div>

</div>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/core.util.js"></script>
<script type="text/javascript" src="js/mlayer.min.js"></script>
<script type="text/javascript">

	$(function(){
		$(".set-text1").before()
		var memberId = CoreUtil.getQueryVariable("memberId");
		var index = mlayer.loading({
			title: '加载中'
		});
		CoreUtil.sendPost("/api/user/getMemberAddress", {"memberId":memberId},function (res) {
			if (res.code == 200) {
				var content = "";
				for (var member of res.data) {
					content += "<div class='sh-adress-box1'>";
					content += "<a  class='sh-adress-box2' addressId='" + member.id + "'>";
					content += "<span class='sh-adress-text1' >" + member.realName + "</span>";
					content += "<span class='sh-adress-text1 text-r'>" + member.mobile + "</span>";
					content += "<span class='sh-adress-text2'>" + member.address + "</span>";
					content += "</a>";
					content += "<div class='sh-adress-box3'>";
					if(member.isDefault == 0) {
						content += "<div class='sh-adress-text3 img2' addressId='" + member.id + "' isDefault = '" + member.isDefault + "'>设为默认地址</div>";
					} else {
						content += "<div class='sh-adress-text3' addressId='" + member.id + "' isDefault = '" + member.isDefault + "'>设为默认地址</div>";
					}
					content += "<div class='sh-adress-text4' addressId='" + member.id + "' >删除</div>";
					content += "</div>";
					content += "</div>";
				}
				$("#addAddress").before(content);
			} else {
				mlayer.error({
					title: res.msg,
					time: 3
				});
			}
			mlayer.close(index);
		});

		$("#addAddress").click(function() {
			location.href="receiving-adress.html?memberId=" + memberId;
		});
		var that;
		$(".bigbox").on("click",".sh-adress-text3",function() {
			$(this).toggleClass("img2");
			$(this).parent(".sh-adress-box3").parent(".sh-adress-box1").siblings(".sh-adress-box1")
					.find(".sh-adress-text3").removeClass("img2");
			var isDefault = $(this).attr("isDefault") == 0 ? 1 : 0;
			var addressId = $(this).attr("addressId");
			CoreUtil.sendPost("/api/user/updateMemberAddress", {"memberId":memberId,"isDefault":isDefault,"id":addressId},function (res) {
			});
		});
		$(".bigbox").on("click",".sh-adress-box2",function() {
			var addressId = $(this).attr("addressId");
			location.href="receiving-adress-edit.html?addressId=" + addressId + "&memberId=" + memberId;
		});

		$(".bigbox").on("click",".sh-adress-text4",function() {
			$(".delete-layer-bg").show();
			$(".delete-layer").show();
			$("body").addClass("ovflowhide");
			that = $(this);
		});
		$(".delete-layer-bg").click(function () {
			$(".delete-layer-bg").hide();
			$(".delete-layer").hide();
			$("body").removeClass("ovflowhide");
		});
		$(".other1").click(function () {
			$(".delete-layer-bg").hide();
			$(".delete-layer").hide();
			$("body").removeClass("ovflowhide");
		});
		$("#deletebox").click(function () {
			$(that).parent(".sh-adress-box3")
			var addressId = $(that).attr("addressId");
			console.log(addressId)
			CoreUtil.sendPost("/api/user/deleteMemberAddress", {"id":addressId},function (res) {
				$(".delete-layer-bg").hide();
				$(".delete-layer").hide();
				$("body").removeClass("ovflowhide");
				$(that).parent(".sh-adress-box3").parent(".sh-adress-box1").remove();
			});
		});
	});


</script>
</body>
</html>